home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / TAR.PY < prev    next >
Encoding:
Python Source  |  1999-03-09  |  7.0 KB  |  211 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__='''Simple module for writing tar files
  65.  
  66. $Id: tar.py,v 1.3 1999/03/10 00:14:56 klm Exp $'''
  67. __version__='$Revision: 1.3 $'[11:-2]
  68.  
  69. import sys, time, zlib
  70. try:
  71.     from newstruct import pack
  72. except:
  73.     from struct import pack
  74.     
  75.  
  76. from string import find, join
  77.  
  78. def oct8(i):
  79.     i=oct(i)
  80.     return '0'*(6-len(i))+i+' \0'
  81.  
  82. def oct12(i):
  83.     i=oct(i)
  84.     return '0'*(11-len(i))+i+' '
  85.     
  86. def pad(s,l):
  87.     ls=len(s)
  88.     if ls >= l: raise ValueError, 'value, %s, too wide for field (%d)' % (s,l)
  89.     return s+'\0'*(l-ls)
  90.  
  91. class TarEntry:
  92.  
  93.     def __init__(self, path, data,
  94.                  mode=0644, uid=0, gid=0, mtime=None, typeflag='0',
  95.                  linkname='', uname='jim', gname='system', prefix=''):
  96.         "Initialize a Tar archive entry"
  97.         self.data=data
  98.         if mtime is None: mtime=int(time.time())
  99.         header=join([
  100.             pad(path,      100),
  101.             oct8(mode),
  102.             oct8(uid),
  103.             oct8(gid),
  104.             oct12(len(data)),
  105.             oct12(mtime),
  106.             ' ' * 8,
  107.             typeflag,
  108.             pad(linkname,  100),
  109.             'ustar\0',
  110.             '00',
  111.             pad(uname,      32),
  112.             pad(gname,      32),
  113.             '000000 \0',
  114.             '000000 \0',
  115.             pad(prefix,    155),
  116.             '\0'*12,
  117.             ], '')
  118.         if len(header) != 512: raise 'Bad Header Length', len(header)
  119.         header=(header[:148]+
  120.                 oct8(reduce(lambda a,b: a+b, map(ord,header)))+
  121.                 header[156:])
  122.         self.header=header
  123.  
  124.     def __str__(self):
  125.         data=self.data
  126.         l=len(data)
  127.         if l%512: data=data+'\0'*(512-l%512)
  128.         return self.header+data
  129.             
  130. def tar(entries):
  131.     r=[]
  132.     ra=r.append
  133.     for name, data in entries:
  134.         ra(str(TarEntry(name,data)))
  135.     ra('\0'*1024)
  136.     return join(r,'')
  137.  
  138. def tgz(entries):
  139.     c=zlib.compressobj()
  140.     compress=c.compress
  141.     r=[]
  142.     ra=r.append
  143.     for name, data in entries:
  144.         ra(compress(str(TarEntry(name,data))))
  145.     ra(compress('\0'*1024))
  146.     ra(c.flush())
  147.     return join(r,'')
  148.  
  149. class tgzarchive:
  150.  
  151.     def __init__(self, name, time=None):
  152.         self._f=gzFile('%s.tar' % name, time)
  153.  
  154.     def add(self, name, data):
  155.         self._f.write(str(TarEntry(name,data)))
  156.  
  157.     def finish(self):
  158.         self._f.write('\0'*1024)
  159.         
  160.     def __str__(self):
  161.         return self._f.getdata()
  162.  
  163. class gzFile:
  164.     _l=0
  165.     _crc=zlib.crc32("")
  166.  
  167.     def __init__(self, name, t=None):
  168.         self._c=zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS,
  169.                                  zlib.DEF_MEM_LEVEL, 0)
  170.         if t is None: t=time.time()
  171.         self._r=['\037\213\010\010',
  172.                  pack("<i", int(t)),
  173.                  '\2\377',
  174.                  name,
  175.                  '\0'
  176.                  ]
  177.  
  178.     def write(self, s):
  179.         self._crc=zlib.crc32(s, self._crc)
  180.         self._r.append(self._c.compress(s))
  181.         self._l=self._l+len(s)
  182.  
  183.     def getdata(self):
  184.         r=self._r
  185.         append=r.append
  186.         append(self._c.flush())
  187.         append(pack("<i", self._crc))
  188.         append(pack("<i", self._l))
  189.         return join(r,'')
  190.